/**
 * LoggerManager.java
 * Version: 1.0
 * Created in 06/08/2013
 * Created by Gerson Groth
 * 
 * Copyright
 */
package webcrawler.activities.logging;

import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.LogManager;
import org.apache.log4j.PropertyConfigurator;

/**
 * The Class LoggerManager.
 * 
 * Classe responsável por alterar as propriedades do log4j
 * 
 * Poderá ser usada para alterar qualquer propriedade do log, desde o nome do
 * arquivo de logs, caminho do arquivo, layout...
 * 
 * @author Gerson Groth
 */
public class LoggerManager {

	/** The logger manager. */
	public static LoggerManager LOGGER_MANAGER = new LoggerManager();

	/**
	 * Change log properties.
	 * 
	 * @param key
	 *            the key
	 * @param value
	 *            the value
	 */
	public void changeLogProperties(String key, String value) {
		Properties props = new Properties();
		loadProperty(props);
		props.setProperty(key, value);
		LogManager.resetConfiguration();
		PropertyConfigurator.configure(props);
	}

	/**
	 * Load property.
	 * 
	 * @param props
	 *            the props
	 */
	private void loadProperty(Properties props) {
		try {
			InputStream configStream = getClass().getResourceAsStream(
					"/log4j.properties");
			props.load(configStream);
			configStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Gets the single instance of LoggerManager.
	 * 
	 * @return single instance of LoggerManager
	 */
	public static LoggerManager getInstance() {
		return LOGGER_MANAGER;
	}

}
